/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ztewatch.wearcontacts.loader;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Groups;

import com.ztewatch.wearcontacts.R;
import com.ztewatch.wearcontacts.utils.ContactQuery;

/**
 * A loader for use in the default contact list, which will also query for the
 * user's group if configured to do so.
 */
public class SortContactsLoader extends CursorLoader {
	private Context mContext;
	public SortContactsLoader(Context context) {
		super(context);
		mContext = context;
	}

	public void setLoadProfile(boolean flag) {
	}

	public void setProjection(String[] projection) {
		super.setProjection(projection);
	}

	@Override
	public Cursor loadInBackground() {
		List<Cursor> cursors = new ArrayList<Cursor>();
		Cursor cursor = null;
		try {
			cursor = super.loadInBackground();
		} catch (NullPointerException e) {
		}

		// add history start
		MatrixCursor matrix = new MatrixCursor(ContactQuery.CONTACT_PROJECTION);
		Object[] addHistory = new Object[ContactQuery.CONTACT_PROJECTION.length];
		for (int i = 0; i < addHistory.length; i++) {
			if (Contacts.DISPLAY_NAME_PRIMARY
					.equals(ContactQuery.CONTACT_PROJECTION[i])) {
				addHistory[i] = mContext.getString(R.string.history_str);
			}
		}
		matrix.addRow(addHistory);
		Object[] row = new Object[ContactQuery.CONTACT_PROJECTION.length];
		while (cursor.moveToNext()) {
			for (int i = 0; i < row.length; i++) {
				row[i] = cursor.getString(i);
			}
			matrix.addRow(row);
		}

		// add history end

		final Cursor contactsCursor = cursor;
		cursors.add(matrix);
//		cursors.add(loadGroup());
		return new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) {
			@Override
			public Bundle getExtras() {
				return contactsCursor == null ? new Bundle() : contactsCursor
						.getExtras();
			}
		};
	}

	private MatrixCursor loadGroup() {
		Cursor cursor = getContext().getContentResolver().query(
				GroupListLoaderUtil.GROUP_LIST_URI,
				GroupListLoaderUtil.COLUMNS,
				Groups.ACCOUNT_TYPE + " NOT NULL AND " + Groups.ACCOUNT_NAME
						+ " NOT NULL AND " + Groups.AUTO_ADD + "=0 AND "
						+ Groups.FAVORITES + "=0 AND " + Groups.DELETED + "=0",
				null,
				Groups.ACCOUNT_TYPE + ", " + Groups.ACCOUNT_NAME + ", "
						+ Groups.DATA_SET + ", " + Groups.TITLE
						+ " COLLATE LOCALIZED ASC");
		if (cursor == null) {
			return null;
		}
		try {
			MatrixCursor matrix = new MatrixCursor(GroupListLoaderUtil.COLUMNS);
			Object[] row = new Object[GroupListLoaderUtil.COLUMNS.length];
			while (cursor.moveToNext()) {
				for (int i = 0; i < row.length; i++) {
					row[i] = cursor.getString(i);
				}
				matrix.addRow(row);
			}
			return matrix;
		} finally {
			cursor.close();
		}
	}
}
